import java.util.Random;

/**
 * Created with IntelliJ IDEA
 * Description:第一个多线程程序
 * User:恋恋
 * Date:2023-09-03
 * Time:8:57
 */
public class ThreadDemo {
    //私有静态类，只能在ThreadDemo类中使用，且不能做父类
    private static class MyThread extends Thread{
        @Override
        public void run() {
            Random random = new Random();
            while(true){
                //打印线程的名字
                System.out.println(Thread.currentThread().getName());
                try{
                    //随机停止运行0-9秒
                    Thread.sleep(random.nextInt(10));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        public static void main(String[] args) {
            MyThread T1 = new MyThread();
            MyThread T2 = new MyThread();
            MyThread T3 = new MyThread();
            T1.start();
            T2.start();
            T3.start();
            Random random = new Random();
            while(true){
                //打印线程名称
                System.out.println(Thread .currentThread().getName());
                try {
                    Thread.sleep(random.nextInt(10));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
